package com.gsit.expenditure.controller;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import org.hibernate.exception.DataException;
import com.gsit.expenditure.client.domina.CustomerBean;
import com.gsit.expenditure.client.domina.FXMLBase;
import com.gsit.expenditure.component.CheckBoxTableCell;
import com.gsit.expenditure.domain.Customer;
import com.gsit.expenditure.services.CustomerService;
import com.gsit.expenditure.utils.AppContextLoader;
import com.gsit.expenditure.utils.Page;
import com.gsit.expenditure.utils.ViewHandler;

public class SearchCustomerController extends BaseController implements	Initializable {

	@FXML private TextField customerNameText;
	@FXML private TableView<Customer> customerTable;
	// @FXML
	// private TableColumn<Customer, Boolean> actionColumn;
	@FXML private TableColumn<Customer, String> firstNameColumn;
	@FXML private TableColumn<Customer, String> lastNameColumn;
	@FXML private TableColumn<Customer, String> emailColumn;
	@FXML private TableColumn<Customer, String> phoneColumn;
	@FXML private TableColumn<Customer, String> mobileColumn;
	@FXML private TableColumn<Customer, Boolean> selectColumn;

	private final SearchCustomerController controller = this;

	private CustomerService customerService;
	private CheckBoxTableCell<Customer, Boolean> checkBoxTableCell;
	private final ObservableList<Customer> customers = FXCollections.observableArrayList();

	private List<Customer> customersList;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
		customerService = AppContextLoader.getBean(CustomerService.class);
		checkBoxTableCell = new CheckBoxTableCell<Customer, Boolean>();
		firstNameColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>(	"firstName"));
		lastNameColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("lastName"));
		emailColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("email"));
		phoneColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("phone"));
		mobileColumn.setCellValueFactory(new PropertyValueFactory<Customer, String>("mobile"));
		selectColumn.setCellFactory(new Callback<TableColumn<Customer, Boolean>, TableCell<Customer, Boolean>>() {
			public TableCell<Customer, Boolean> call(TableColumn<Customer, Boolean> p) {
						return new CheckBoxTableCell<Customer, Boolean>();
					}
				});

		selectColumn.setCellValueFactory(new PropertyValueFactory<Customer, Boolean>("select"));

		// customers.
		customerTable.setOnMouseClicked(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {

				if (event.getButton().equals(MouseButton.PRIMARY)) {
					if (event.getClickCount() == 2) {
						
						Customer customer = customerTable.getSelectionModel().getSelectedItem();
						if (customer != null) {
							
							final FXMLBase<CustomerController> fxmlBase = ViewHandler.createFXMLBase(Page.CREATE_CUSTOMER);
							CustomerBean customerBean = new CustomerBean(null);
							customerBean.reLoad(customer);
							fxmlBase.getController().reLoad(customerBean);
							ViewHandler.createDialog(fxmlBase,	"Update Customer......");
							fxmlBase.getController().getStateChange().addListener(new ChangeListener<Boolean>() {

										@Override
										public void changed(
											ObservableValue<? extends Boolean> arg0,Boolean oldValue,Boolean newValue) {
											if (newValue) {
												fxmlBase.getDialogStage().hide();
												controller.onSearch();
											}

										}

									});
							fxmlBase.getDialogStage().showAndWait();
						}
					}
				}

			}
		});

		/*
		 * customerTable.getSelectionModel().selectedItemProperty().addListener(new
		 * ChangeListener<Customer>() {
		 * 
		 * @Override public void changed(ObservableValue<? extends Customer>
		 * observable, Customer oldValue, Customer newValue) {
		 * System.out.println(">>>>>>>>>>>new value >>>>>>>>>"+
		 * newValue.getFirstName()); } });
		 */
		
		onSearch();

	}

	@Override
	public void onLoad() {

		if (customersList != null) {
			customerTable.getItems().clear();
			customers.clear();
			customers.addAll(customersList);
			customerTable.setItems(customers);
		}
	}

	@Override
	public void onSearch() {

		String customerName = customerNameText.getText();

		if (customerName != null && !customerName.trim().equals("")) {
			customersList = customerService.findByName(customerName);
		} else {
			customersList = customerService.findAll();
		}

		onLoad();
		
	}

	@Override
	public void onDelete() throws DataException{

		Set<Customer> custSet = (Set<Customer>) checkBoxTableCell.getSelectedItemSet();

		if (custSet != null) {
			
			for (Customer customer : custSet) {
				customerService.delete(customer);
			}

		}

		CheckBoxTableCell.setSelectedItemSet(null);
		onSearch();
	}

}
